package ch01.thread;

/**
 *
 * Created by zc on 2016/11/7.
 */
public class ProductConsume {

    public static void main(String[] args){
        Info info=new Info();
        Product pro=new Product(info);
        Consume con=new Consume(info);
        new Thread(pro).start();
        new Thread(con).start();
    }

}

class Consume implements Runnable {
    private Info info;

    public Consume(Info info) {
        this.info = info;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++)
        {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.info.getName()+"---饰演--->"+this.info.getContent());
        }
    }
}

class Product implements Runnable {
    private Info info;

    public Product(Info info) {
        this.info = info;
    }

    @Override
    public void run() {
        boolean flag = true;
        for(int i=0; i<10; i++){
            if(flag){
                this.info.setName("Name A");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.info.setContent("Context A");
                flag = false;
            } else {
                this.info.setName("Name B");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.info.setContent("Context B");
                flag = true;
            }
        }
    }
}
class Info {
    private String name;
    private String content;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}